package mobidever.mygodutch.activity;

import mobidever.mygodutch.R;
import mobidever.mygodutch.R.layout;
import mobidever.mygodutch.activity.base.ActivityFrame;
import mobidever.mygodutch.adapter.AdapterCategory;
import mobidever.mygodutch.adapter.AdapterAppGrid;
import mobidever.mygodutch.adapter.AdapterUser;
import mobidever.mygodutch.bussiness.BussinessCategory;
import mobidever.mygodutch.bussiness.BussinessUser;
import mobidever.mygodutch.controls.SliderItem;
import mobidever.mygodutch.controls.SliderMenu.OnSliderMenuClickListener;
import mobidever.mygodutch.model.ModelCategory;
import mobidever.mygodutch.model.ModelUser;
import mobidever.mygodutch.utility.RegexTool;
import android.R.integer;
import android.R.string;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class ActivityCategory extends ActivityFrame implements
		OnSliderMenuClickListener
{

	private ExpandableListView mCategoryList;
	private AdapterCategory mAdapterCategory;
	private BussinessCategory mBussinessCategory;
	private ModelCategory mSelectModelCategory;
	private final int  mRequestCode = 1;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		AppendMainBody(R.layout.category);
		initVariable();
		initView();
		initListener();
		bindData();
		createSlideMenu(R.array.SlideMenuCategory);
	}

	private void initVariable()
	{
		mAdapterCategory = new AdapterCategory(this);
		mBussinessCategory = new BussinessCategory(this);
	}

	private void initView()
	{
		mCategoryList = (ExpandableListView) findViewById(R.id.elvCategory);
	}

	private void initListener()
	{
		registerForContextMenu(mCategoryList);
	}

	private void bindData()
	{
		mAdapterCategory = new AdapterCategory(this);
		mCategoryList.setAdapter(mAdapterCategory);
	}

	private boolean deleteCategory(ModelCategory pModelCategory)
	{
		String _Message = getString(R.string.DialogMessageCategoryDelete, new Object[] {pModelCategory.getCategoryName()});
		return AlertDailogDeleteOrCanle(_Message, new OnEditOrDeleteListner());
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo)
	{
		ExpandableListContextMenuInfo _AdapterContextMenuInfo = (ExpandableListContextMenuInfo) menuInfo;

		long _Position = _AdapterContextMenuInfo.packedPosition;
		int _Type = ExpandableListView.getPackedPositionType(_Position);
		int _GroupPosition = ExpandableListView
				.getPackedPositionGroup(_Position);

		switch (_Type)
		{
		case ExpandableListView.PACKED_POSITION_TYPE_GROUP:
			mSelectModelCategory = (ModelCategory) mAdapterCategory
					.getGroup(_GroupPosition);
			break;
		case ExpandableListView.PACKED_POSITION_TYPE_CHILD:
			int _ChildrenPosition = ExpandableListView
					.getPackedPositionChild(_Position);
			mSelectModelCategory = (ModelCategory) mAdapterCategory.getChild(
					_GroupPosition, _ChildrenPosition);
			break;

		default:
			break;
		}

		menu.setHeaderIcon(R.drawable.account_book_small_icon);
		if (mSelectModelCategory != null)
		{
			menu.setHeaderTitle(mSelectModelCategory.getCategoryName());
		}

		creatMenu(menu);
		menu.add(0, 3, 3, R.string.ActivityCategoryTotal);
		if (mAdapterCategory.getChildCountOfGroup(_GroupPosition) != 0
				& mSelectModelCategory.getParentID() == 0)
		{
			menu.findItem(2).setEnabled(false);
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		bindData();
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case 1:
			Intent _Intent = new Intent();
			_Intent.putExtra("ModelCategory", mSelectModelCategory);
			_Intent.setClass(ActivityCategory.this, ActivityCategoryAddOrEdit.class);
			startActivityForResult(_Intent, mRequestCode);
			break;
		case 2:
			deleteCategory(mSelectModelCategory);
			break;
		case 3:
			break;
		default:
			break;
		}
		return super.onContextItemSelected(item);
	}
	
	

	public void onSliderMenuClick(View pView, SliderItem pSliderItem)
	{
		if (pSliderItem.getItemID() == 0)
		{
			SlideMenuToggle();
			Intent _Intent = new Intent();
			_Intent.setClass(ActivityCategory.this, ActivityCategoryAddOrEdit.class);
			startActivityForResult(_Intent, mRequestCode);
		}
	}

	



	private class OnEditOrDeleteListner implements
			DialogInterface.OnClickListener
	{

		public void onClick(DialogInterface dialog, int which)
		{
			boolean _Result = mBussinessCategory
					.deleteModelCategoryByID((int) mSelectModelCategory
							.getCategoryID());
			if (_Result)
			{
				bindData();
				showMsg(getString(R.string.TipsDeleteSeucceed));
			} else
			{
				showMsg(getString(R.string.TipsDeleteFail));
			}
		}
	}

}
